import "reflect-metadata";

export function Singleton(isSingleton: boolean): PropertyDecorator {
  return (targetClassPrototype, propertyKey) => {
    const PropClass = Reflect.getMetadata(
      "design:type",
      targetClassPrototype,
      propertyKey
    );
    // console.log(PropClass);
    const getStaticMethod = PropClass.getServiceImplClass();
    let getClassOrInstance: any;
    const metaSignleton = Reflect.getMetadata(
      "signleton",
      targetClassPrototype,
      propertyKey
    );

    if (isSingleton) {
      if (!metaSignleton) {
        Reflect.defineMetadata(
          "signleton",
          isSingleton,
          targetClassPrototype,
          propertyKey
        );
        getClassOrInstance = getStaticMethod.getInstance();
      } else {
        console.log("单例模式已经创建，使用上一次的对象");
      }
    } else {
      getClassOrInstance = getStaticMethod;
    }
    Reflect.defineMetadata(
      "getClassOrInstance",
      getClassOrInstance,
      targetClassPrototype,
      propertyKey
    );
  };
}
